package server.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.business.pojo.BloodDiscardReview;
import server.business.service.BloodDiscardApplyService;
import server.business.service.BloodDiscardReviewService;
import server.business.service.BloodInformationService;
import server.business.utils.results.lrz.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


@Slf4j
@RestController
@RequestMapping("/audit")
public class BloodDiscardAuditController {
    @Autowired
    private BloodDiscardApplyService bloodDiscardApplyService;
    @Autowired
    private BloodInformationService bloodInformationService;

    @Autowired
    private BloodDiscardReviewService bloodDiscardReviewService;

    @GetMapping("/review/page")
    @ResponseBody
    public IPage<BloodDiscardReview> findPage1(HttpServletRequest request, HttpServletResponse response){
        Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        IPage<BloodDiscardReview> page = new Page<>(pageNum,pageSize);
        QueryWrapper<BloodDiscardReview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        //总查询
        return bloodDiscardReviewService.page(page,queryWrapper);
    }

    @GetMapping("/review/querypage")
    @ResponseBody
    public IPage<BloodDiscardReview> findPage(HttpServletRequest request, HttpServletResponse response){
        Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String discardapplyid= request.getParameter("discardapplyid");
        String applyid = request.getParameter("applyid");
        String endTime = request.getParameter("endTime");
        String startTime = request.getParameter("startTime");
        String state = request.getParameter("state");
        IPage<BloodDiscardReview> page = new Page<>(pageNum,pageSize);
        QueryWrapper<BloodDiscardReview> queryWrapper = new QueryWrapper<>();
        //如果id被填写
        if (discardapplyid != ""&& discardapplyid != null) {
            queryWrapper.like("blood_discard_id",discardapplyid);
        }
        if (applyid != "" && applyid != null ){
            queryWrapper.eq("applicant_id",applyid);
        }
        if(state != "" && state != null){
            queryWrapper.eq("state",state);
        }
        if (startTime !="" && startTime!= null){
            queryWrapper.ge("apply_time",startTime);
        }
        if (endTime != "" && endTime!= null){
            queryWrapper.le("apply_time",endTime);
        }
        queryWrapper.eq("is_deleted",0);
        return bloodDiscardReviewService.page(page,queryWrapper);

    }

    @GetMapping("/review/getselectapply")
    @ResponseBody
    public Result getSelectIdInformation(@RequestParam("discardapplyid") String discardapplyid){
        log.info(discardapplyid);
        log.info("--------------------------");
        return Result.success(bloodDiscardApplyService.findByApplyId(discardapplyid));
    }

    @GetMapping("/review/success")
    @ResponseBody
    public boolean Audit(@RequestParam("discardapplyid") String discardapplyid, @RequestParam("bloodId") String bloodId,
                         @RequestParam("reviewerId") String reviewerId) {
        bloodInformationService.setStateByBloodNumber(bloodId,"14");
        bloodDiscardApplyService.setisDeletedByApplyId(discardapplyid,1);
        bloodDiscardReviewService.setreviewidbyDiscardId(discardapplyid,reviewerId);
        bloodDiscardReviewService.setauditdaybyDiscardId(discardapplyid,new Date());
        bloodDiscardReviewService.setapplystatebyDiscardId(discardapplyid,"02");
        bloodDiscardReviewService.setisDeletedById(discardapplyid,1);
        return  true;
    }
    @GetMapping("/review/rejectsuccess")
    @ResponseBody
    public boolean reject(@RequestParam("discardapplyid") String discardapplyid, @RequestParam("bloodId") String bloodId,
                         @RequestParam("reviewerId") String reviewerId, @RequestParam("rejectReason") String rejectReason) {
        bloodInformationService.setStateByBloodNumber(bloodId,"2");
        bloodDiscardApplyService.setisDeletedByApplyId(discardapplyid,1);
        bloodDiscardReviewService.setreviewidbyDiscardId(discardapplyid,reviewerId);
        bloodDiscardReviewService.setauditdaybyDiscardId(discardapplyid,new Date());
        bloodDiscardReviewService.setapplystatebyDiscardId(discardapplyid,"03");
        bloodDiscardReviewService.setrejectreasonbyDiscardId(discardapplyid,rejectReason);
        bloodDiscardReviewService.setisDeletedById(discardapplyid,1);
        return  true;
    }
}
